/*    */ package com.zimbra.cs.service.mail;
/*    */ 
/*    */ import com.zimbra.common.mime.shim.JavaMailInternetAddress;
/*    */ import com.zimbra.common.service.ServiceException;
/*    */ import com.zimbra.common.soap.Element;
/*    */ import com.zimbra.common.soap.MailConstants;
/*    */ import com.zimbra.common.util.L10nUtil;
/*    */ import com.zimbra.common.util.L10nUtil.MsgKey;
/*    */ import com.zimbra.common.util.Log;
/*    */ import com.zimbra.common.util.LruMap;
/*    */ import com.zimbra.common.util.ZimbraLog;
/*    */ import com.zimbra.cs.account.Account;
/*    */ import com.zimbra.cs.mailbox.MailSender;
/*    */ import com.zimbra.cs.mailbox.Mailbox;
/*    */ import com.zimbra.cs.mime.Mime.FixedMimeMessage;
/*    */ import com.zimbra.cs.util.JMSession;
/*    */ import com.zimbra.soap.ZimbraSoapContext;
/*    */ import java.security.SecureRandom;
/*    */ import java.util.Collections;
/*    */ import java.util.Map;
/*    */ import javax.mail.Message.RecipientType;
/*    */ import javax.mail.MessagingException;
/*    */ import javax.mail.internet.MimeMessage;
/*    */ import org.apache.commons.codec.binary.Hex;
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ public class SendVerificationCode
/*    */   extends MailDocumentHandler
/*    */ {
/* 47 */   static Map<String, String> emailToCodeMap = Collections.synchronizedMap(new LruMap(1000));
/*    */   
/*    */   public Element handle(Element request, Map<String, Object> context) throws ServiceException
/*    */   {
/* 51 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*    */     
/* 53 */     String emailAddr = request.getAttribute("a");
/* 54 */     String code = generateVerificationCode();
/*    */     try {
/* 56 */       sendVerificationCode(emailAddr, code, getRequestedMailbox(zsc));
/*    */     } catch (MessagingException e) {
/* 58 */       throw ServiceException.FAILURE("Error in sending verification code to device", e);
/*    */     }
/* 60 */     emailToCodeMap.put(emailAddr, code);
/* 61 */     return zsc.createElement(MailConstants.SEND_VERIFICATION_CODE_RESPONSE);
/*    */   }
/*    */   
/*    */   static void sendVerificationCode(String emailAddr, String code, Mailbox mbox) throws MessagingException, ServiceException {
/* 65 */     MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSmtpSession(mbox.getAccount()));
/* 66 */     mm.setRecipient(Message.RecipientType.TO, new JavaMailInternetAddress(emailAddr));
/* 67 */     mm.setText(L10nUtil.getMessage(L10nUtil.MsgKey.deviceSendVerificationCodeText, mbox.getAccount().getLocale(), new Object[] { code }), "utf-8");
/*    */     
/*    */ 
/* 70 */     mm.saveChanges();
/* 71 */     MailSender mailSender = mbox.getMailSender();
/* 72 */     mailSender.setSaveToSent(false);
/* 73 */     mailSender.sendMimeMessage(null, mbox, mm);
/*    */   }
/*    */   
/*    */   static String generateVerificationCode() {
/* 77 */     ZimbraLog.misc.debug("Generating verification code");
/* 78 */     SecureRandom random = new SecureRandom();
/* 79 */     byte[] bytes = new byte[3];
/* 80 */     random.nextBytes(bytes);
/* 81 */     return new String(Hex.encodeHex(bytes));
/*    */   }
/*    */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/SendVerificationCode.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */